# 字典 -- dict
# 字典是无序,可变的数据类型
# 字典:用于存储数据,存储大量数据 字典要比列表快 将数据和数据之间进行关联

# lst1 = ["常鑫","李文虎","张宇"] # 姓名
# lst2 = [10,80,100] #分数
# print(lst1[0],lst2[0])

# 定义一个字典:
# dic = {"常鑫":10,"李文虎":80,"张宇":100}
# 字典中逗号分隔叫做一个元素 "常鑫"(键):10(值)  -- 键值对
# dic = {"常鑫":10,"李文虎":80,"张宇":100,10:"大黑哥",True:123,(1,2,3):"大煎饼",[1,2]:"meet"}
# print(dic)

# 字典的键:
#     可哈希的 -- 不可变的数据类型  -- 避免哈希冲突使用了 开放寻址法
#     不可哈希的 -- 可变的数据类型
#     要求唯一  -- 如果有重复的后边值的将前面的值覆盖

# 字典的值:
#     可以任意

# dic = {"常鑫":[1,2,3],"李文虎":80,"张宇":100,10:"大黑哥",True:123,(1,2,3):"大煎饼","常鑫":0.5}
# print(dic)

# dic = {"常鑫":[1,2,3],'李文虎': '89'}
# 增:
# dic["李文虎"] = 89  # 暴力添加
# print(dic)

# dic.setdefault("李文虎",100)
# # 1.李文虎这个键去字典进行查询,如果不存在执行添加,如果存在就不添加了
# print(dic)
# print(dic.setdefault("李文虎"))

# 删除:

# del dic["常鑫"]   # 通过字典的键删除键值对
# print(dic)

# dic = {"常鑫":[1,2,3],'李文虎': '89'}
# print(dic.pop("常鑫"))  #通过字典的键进行删除键值对  返回值:返回的是被删除的值
# print(dic)

# dic = {"常鑫":[1,2,3],'李文虎': '89'}
# dic1 = {"1":3,"5":9,"李文虎":100}
# dic.clear()  # 清空
# print(dic)

# 改:
# dic["常鑫"] = "dsb"   # 字典中没有某个键值对的时候就是添加,当键在字典中存在的时候就是修改
# print(dic)            #有则改 无则加

# dic1.update(dic)  #  update括号中的字典级别高于update前面的字典
# print(dic1)

# 查
# dic1 = {"1":3,"5":9,"李文虎":100}
# for i in dic1:   # 循环获取的是字典中每个键值对的键
#     print(i)

# print(dic.get("cx"))  # 通过键获取值,如果将不存在字典中返回None
# print(dic["cx"])      # 通过键获取值,如果将不存在字典中报错

# 其他操作
dic1 = {"1":3,"5":9,"李文虎":100}

# for i in dic1:
#     print(i,dic1.get(i))   # 获取所有键和值

# print(dic1.keys())  # 高仿列表 -- dict_keys(['1', '5', '李文虎']) 支持for循环 不支持索引

# for i in dic1.keys():       # 获取字典中所有的键
#     print(i)

# print(dic1.keys()[0]

# lst = []
# for i in dic1:
#     lst.append(dic1[i])

# print(list(dic1.values()))   # 获取字典中所有的值

# print(dic1.items())

# for i in dic1.items():
#     print(i[0],i[1])

# 解构
# a,b = 10,12
# print(a)
# print(b)

# a = 10
# b = 20
# a,b = b,a
# print(a,b)

# a,b = [1,2]
# print(a,b)

# a,b = (3,4)
# print(a,b)

# a,_,b = (3,4,5)
# print(a,b)   # 等号后边的值和前面的变量名要一一对应

# a = 10,12  # 本质就是一个元组
# print(a)

# a,b = "23"
# print(a,b)

# a,b = {"1":"a","2":"b"}
# print(a,b)

# for i in dic1.items():
#     print(i)

# for k,v in dic1.items():
#     print(k,v)    # 获取每个元素的键和值

# 字典的嵌套

dic = {1001:["周杰伦","林俊杰"],
       1002:{"汪峰":{"前妻1":["大烧饼"],"前妻2":["熊大","熊二"],"国际章":"肉丝"}},
       1003:{"陈冠希":["阿娇","阿萨","张柏芝"]},
       1004:{"韦小宝":[{"教主夫人":["教主"]},
                    {"阿珂":"刺客"},
                    {"双儿":"天地会"},
                    {"爱心觉罗建宁":"公主"},
                    {"独臂神尼":{"韦小宝师父":{"兰陵王":"隐身"},"花木兰":[
                           "唧唧复唧唧","木兰是战士",{"爸爸":["花和尚"]}]
                             }},
                    {"陈圆圆":"丈母娘"},{"穆建平":"小郡主"}]},
       1005:{"常鑫":["大黑哥","大烧饼","吃大煎饼","肉夹馍","自行车","井盖",]}
       }


# print(dic)
# print(dic[1004]["韦小宝"][0]["教主夫人"][0])
# print(dic[1005]["常鑫"][-1])
# print(dic[1002]["汪峰"]["前妻2"][-1])
# print(dic[1004]["韦小宝"][4]["独臂神尼"]["韦小宝师父"]["兰陵王"])
# print(dic[1004]["韦小宝"][4]["独臂神尼"]["花木兰"][2]["爸爸"][0])